/**
 * Created by George on 2018/1/27.
 * activityCtrl
 */
define(['angular', 'util'], function (angular, util) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var activityCtrl = function ($scope, $http) {
        $scope.welcome_info = "活动管理";
        $scope.activityRecordDatas = [];

        function init() {
            initOrUpdateData();
        }

        function initOrUpdateData() {
            $http.get("/basketballfire/rest/activity/list").then(function (data, status) {
                console.log(data);
                $scope.activityRecordDatas = data.data.data;
                for (var index = 0; index < $scope.activityRecordDatas.length; index++) {
                    var activityRecord = $scope.activityRecordDatas[index];
                    var usersDesc = "";
                    for (var i = 0; i < activityRecord.records.length; i++) {
                        if (activityRecord.records[i].userNum > 1) {
                            usersDesc += " ;" + activityRecord.records[i].userName + " x " + activityRecord.records[i].userNum;
                        } else {
                            usersDesc += " ;" + activityRecord.records[i].userName;
                        }
                    }
                    usersDesc = usersDesc.substr(2);
                    activityRecord.usersDesc = usersDesc;
                }
            });
        }

        $scope.deleteActivity = function (activity) {
            var msg = "您真的确定要删除吗？";
            if (!confirm(msg)) {
                return;
            }
            $http.delete('/basketballfire/rest/activity?id=' + activity.id).then(function () {
                console.log("success!");
                alert("删除成功！");
                initOrUpdateData();
            }, function () {
                console.log("failed!");
                alert("删除失败！");
            });
        };

        //app start
        init();
    };

    return activityCtrl;
});